Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][analyzer] Less redundant warnings from FixedAddressChecker #110458

Merged
merged 2 commits into from
Oct 3, 2024

Conversation

balazske
Copy link
Collaborator

If a fixed value is assigned to a pointer variable, the checker did emit a warning. If the pointer variable is assigned to another pointer variable, this resulted in another warning. The checker now emits warning only if a value with non-pointer type is assigned (to a pointer variable).

If a fixed value is assigned to a pointer variable, the checker
did emit a warning. If the pointer variable is assigned to another
pointer variable, this resulted in another warning.
The checker now emits warning only if a value with non-pointer type is assigned
(to a pointer variable).
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Sep 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2024

@llvm/pr-subscribers-clang

Author: Balázs Kéri (balazske)

Changes

If a fixed value is assigned to a pointer variable, the checker did emit a warning. If the pointer variable is assigned to another pointer variable, this resulted in another warning. The checker now emits warning only if a value with non-pointer type is assigned (to a pointer variable).


Full diff: https://github.com/llvm/llvm-project/pull/110458.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp (+6)
  • (modified) clang/test/Analysis/ptr-arith.c (+4)
diff --git a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
index e7fde3edc7f9ee..87ba8e070b328a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
@@ -43,6 +43,12 @@ void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
   if (!T->isPointerType())
     return;
 
+  // Omit warning if the RHS has already pointer type.
+  // The value may come from a variable and is candidate for a previous warning
+  // from the checker.
+  if (B->getRHS()->IgnoreParenCasts()->getType()->isPointerType())
+    return;
+
   SVal RV = C.getSVal(B->getRHS());
 
   if (!RV.isConstant() || RV.isZeroConstant())
diff --git a/clang/test/Analysis/ptr-arith.c b/clang/test/Analysis/ptr-arith.c
index 020a5006292306..b5ccd2c207ead1 100644
--- a/clang/test/Analysis/ptr-arith.c
+++ b/clang/test/Analysis/ptr-arith.c
@@ -42,6 +42,10 @@ domain_port (const char *domain_b, const char *domain_e,
 void f4(void) {
   int *p;
   p = (int*) 0x10000; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
+
+  int *p1;
+  p1 = p; // no warning
+
   long x = 0x10100;
   x += 10;
   p = (int*) x; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}

@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2024

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Balázs Kéri (balazske)

Changes

If a fixed value is assigned to a pointer variable, the checker did emit a warning. If the pointer variable is assigned to another pointer variable, this resulted in another warning. The checker now emits warning only if a value with non-pointer type is assigned (to a pointer variable).


Full diff: https://github.com/llvm/llvm-project/pull/110458.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp (+6)
  • (modified) clang/test/Analysis/ptr-arith.c (+4)
diff --git a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
index e7fde3edc7f9ee..87ba8e070b328a 100644
--- a/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
@@ -43,6 +43,12 @@ void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
   if (!T->isPointerType())
     return;
 
+  // Omit warning if the RHS has already pointer type.
+  // The value may come from a variable and is candidate for a previous warning
+  // from the checker.
+  if (B->getRHS()->IgnoreParenCasts()->getType()->isPointerType())
+    return;
+
   SVal RV = C.getSVal(B->getRHS());
 
   if (!RV.isConstant() || RV.isZeroConstant())
diff --git a/clang/test/Analysis/ptr-arith.c b/clang/test/Analysis/ptr-arith.c
index 020a5006292306..b5ccd2c207ead1 100644
--- a/clang/test/Analysis/ptr-arith.c
+++ b/clang/test/Analysis/ptr-arith.c
@@ -42,6 +42,10 @@ domain_port (const char *domain_b, const char *domain_e,
 void f4(void) {
   int *p;
   p = (int*) 0x10000; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
+
+  int *p1;
+  p1 = p; // no warning
+
   long x = 0x10100;
   x += 10;
   p = (int*) x; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}

Copy link
Contributor

@NagyDonat NagyDonat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, nice little improvement :)

I reworded a comment in an inline suggestion because I felt that it was a bit difficult to understand; but feel free to change it further if you'd prefer.

clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp Outdated Show resolved Hide resolved
Co-authored-by: Donát Nagy <donat.nagy@ericsson.com>
Copy link
Contributor

@NagyDonat NagyDonat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, I think you can merge this now.

@balazske balazske merged commit 1701895 into llvm:main Oct 3, 2024
6 of 8 checks passed
@balazske balazske deleted the fixedaddress_noredundantwarning branch October 3, 2024 07:18
xgupta pushed a commit to xgupta/llvm-project that referenced this pull request Oct 4, 2024
…lvm#110458)

If a fixed value is assigned to a pointer variable, the checker did emit
a warning. If the pointer variable is assigned to another pointer
variable, this resulted in another warning. The checker now emits
warning only if a value with non-pointer type is assigned (to a pointer
variable).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants